﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;

using DataAccess;
using Entities;
using Common;

namespace BusinessLogic
{
    static class ChiTietKhoHang
    {
        public static Entities.ChiTietKhoHang[] SelectChiTietKhoHang(Entities.ChiTietKhoHang hoadon)
        {
            Entities.ChiTietKhoHang[] list = null;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.ChiTietKhoHang.sp_Select_ChiTietKhoHang, cn);
                cmd.Parameters.Add(Common.Constants.ChiTietKhoHang.HanhDong, SqlDbType.NVarChar).Value = hoadon.HanhDong;
                cmd.Parameters.Add(Common.Constants.ChiTietKhoHang.MaKho, SqlDbType.VarChar).Value = hoadon.MaKho;
                cmd.Parameters.Add(Common.Constants.ChiTietKhoHang.MaHangHoa, SqlDbType.VarChar).Value = hoadon.MaHangHoa;
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.ChiTietKhoHang row = new Entities.ChiTietKhoHang();
                    row.MaKho = dr[Common.Constants.ChiTietKhoHang.MaKho].ToString();
                    row.TenKho = dr[Common.Constants.ChiTietKhoHang.TenKho].ToString();
                    row.MaHangHoa = dr[Common.Constants.ChiTietKhoHang.MaHangHoa].ToString();
                    row.TenHangHoa = dr[Common.Constants.ChiTietKhoHang.TenHangHoa].ToString();
                    row.SoLuong = dr[Common.Constants.ChiTietKhoHang.SoLuong].ToString();
                    row.NgayNhap = DateTime.Parse(dr[Common.Constants.ChiTietKhoHang.NgayNhap].ToString());
                    row.HanSuDung = DateTime.Parse(dr[Common.Constants.ChiTietKhoHang.HanSuDung].ToString());
                    row.MaDonViTinh = dr[Common.Constants.ChiTietKhoHang.MaDonViTinh].ToString();
                    row.TenDonViTinh = dr[Common.Constants.ChiTietKhoHang.TenDonViTinh].ToString();
                    row.GhiChu = dr[Common.Constants.ChiTietKhoHang.GhiChu].ToString();
                    row.Deleted = Boolean.Parse(dr[Common.Constants.ChiTietKhoHang.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = null;
                }
                else
                {
                    list = new Entities.ChiTietKhoHang[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.ChiTietKhoHang)arr[i];
                    }
                }
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                list = null;
            }
            return list;
        }
        public static int InsertUpdateListChiTietKhoHang(Entities.ChiTietKhoHang[] hoadon)
        {
            int f = 0;
            try
            {
                for (int i = 0; i < hoadon.Length; i++)
                {
                    f = BusinessLogic.ChiTietKhoHang.InsertUpdateChiTietKhoHang(hoadon[i]);
                    if (f == 0)
                    { break; }
                    else
                    { continue; }
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                f = 0;
            }
            return f;
        }
        public static int InsertUpdateChiTietKhoHang(Entities.ChiTietKhoHang hoadon)
        {
            int f = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.ChiTietKhoHang.sp_InsertUpdateChiTietKhoHang, cn);
                cmd.Parameters.Add(Common.Constants.ChiTietKhoHang.HanhDong, SqlDbType.NVarChar).Value = hoadon.HanhDong;
                cmd.Parameters.Add(Common.Constants.ChiTietKhoHang.MaKho, SqlDbType.VarChar).Value = hoadon.MaKho;
                cmd.Parameters.Add(Common.Constants.ChiTietKhoHang.TenKho, SqlDbType.NVarChar).Value = hoadon.TenKho;
                cmd.Parameters.Add(Common.Constants.ChiTietKhoHang.MaHangHoa, SqlDbType.VarChar).Value = hoadon.MaHangHoa;
                cmd.Parameters.Add(Common.Constants.ChiTietKhoHang.TenHangHoa, SqlDbType.NVarChar).Value = hoadon.TenHangHoa;
                cmd.Parameters.Add(Common.Constants.ChiTietKhoHang.SoLuong, SqlDbType.Float).Value = hoadon.SoLuong;
                cmd.Parameters.Add(Common.Constants.ChiTietKhoHang.NgayNhap, SqlDbType.DateTime).Value = hoadon.NgayNhap;
                cmd.Parameters.Add(Common.Constants.ChiTietKhoHang.HanSuDung, SqlDbType.DateTime).Value = hoadon.HanSuDung;
                cmd.Parameters.Add(Common.Constants.ChiTietKhoHang.MaDonViTinh, SqlDbType.VarChar).Value = hoadon.MaDonViTinh;
                cmd.Parameters.Add(Common.Constants.ChiTietKhoHang.TenDonViTinh, SqlDbType.NVarChar).Value = hoadon.TenDonViTinh;
                cmd.Parameters.Add(Common.Constants.ChiTietKhoHang.GhiChu, SqlDbType.VarChar).Value = hoadon.GhiChu;
                cmd.Parameters.Add(Common.Constants.ChiTietKhoHang.Deleted, SqlDbType.Bit).Value = hoadon.Deleted;
                f = cmd.ExecuteNonQuery();
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                f = 0;
            }
            return f;
        }
        public static int Delete(Entities.ChiTietKhoHang kho)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.ChiTietKhoHang.sp_Delete_ChiTietKhoHang, cn);
                cmd.Parameters.Add(Common.Constants.ChiTietKhoHang.MaKho, SqlDbType.VarChar).Value = kho.MaKho;
                cmd.Parameters.Add(Common.Constants.ChiTietKhoHang.MaHangHoa, SqlDbType.VarChar).Value = kho.MaHangHoa;
                rt = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); rt = 0; }
            return rt;
        }
    }
}
